{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAXcAAAD8CAYAAACMwORRAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADl0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uIDMuMC4yLCBodHRwOi8vbWF0cGxvdGxpYi5vcmcvOIA7rQAAHHJJREFUeJzt3X+M3ddZ5/H3k7FdprvFU3DQEtupza4b4bqGVLNpkaVtqUvzg80PVcEkVcWyimoVCNGWYq2rSq5ltKppxUZEigCrRAWkbXCqrndK3U21catCICUTuXETo7DeFMg4FXGX2H9sHTpxnv3je8e5c31/fO+93x/nx+clRZ65883MOXfmPvd8z3nOc8zdERGRtFzVdgNERKR6Cu4iIglScBcRSZCCu4hIghTcRUQSpOAuIpIgBXcRkQQpuIuIJEjBXUQkQWva+sEbNmzwLVu2tPXjRUSi9NRTT33P3a8edV1rwX3Lli0sLi629eNFRKJkZn9f5rqR0zJm9pCZvWRmzwz4upnZA2Z2xsxOmdk7xm2siIhUq8yc++eAm4Z8/WZgW+e/vcDvTd8sERGZxsjg7u7fAP5pyCW3A3/shSeAOTP78aoaKCIi46siW2Yj8ELX50udx65gZnvNbNHMFs+dO1fBjxYRkX6qCO7W57G+ReLd/Yi7z7v7/NVXj1zsFRGRCVUR3JeAzV2fbwJerOD7iojIhKoI7gvAL3WyZt4FXHD371bwfUVEZEIj89zN7PPAe4ANZrYEfBJYC+Duvw8cB24BzgDfB/5jXY0VEZFyRgZ3d797xNcd+LXKWiQSqGMnz/KZR5/jxfMXuWZuln03Xscd1/fNHRBpXWs7VEVicuzkWT7+xW9zcfkSAGfPX+TjX/w2gAK8BEmFw0RK+Myjz10O7CsuLl/iM48+11KLRIZTcBcp4cXzF8d6XKRtCu4iJVwzNzvW4yJtU3AXKWHfjdcxu3Zm1WOza2fYd+N1L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mP0kR3M812srmLQC/1MmaeRdwwd2/O/V3bXslecgK8y3A31Ksrn+i89ghihc2FL/0R4AzwF8DP9F2mxvo8/8C/hH4Vue/hbbb3ES/e679OpFny5T8XRvwX4HTwLeBu9puc0P93g48TpFJ8y3g/W23uYI+fx74LrBMMUq/B/gI8JGu3/WDnefk21X9fWuHqohIgkKdlhERkSkouIuIJEjBXUQkQQruIiIJUnAXEUmQgruISIIU3EVEEqTgLiKSoP8PnAZMmI/R3iAAAAAASUVORK5CYII=\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "\n",
    "#读取数据并作图\n",
    "train = np.genfromtxt('hw7_train.dat')\n",
    "X_train, y_train = train[:, :-1], train[:, -1]\n",
    "test = np.genfromtxt('hw7_test.dat')\n",
    "X_test, y_test = test[:, :-1], test[:, -1]\n",
    "\n",
    "#作图\n",
    "plt.scatter(train[:, 0][train[:, 2] == -1], train[:, 1][train[:, 2] == -1])\n",
    "plt.scatter(train[:, 0][train[:, 2] == 1], train[:, 1][train[:, 2] == 1])\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {},
   "outputs": [],
   "source": [
    "#Gini index\n",
    "def Gini(y):\n",
    "    '''\n",
    "    二分类gini系数\n",
    "    '''\n",
    "    if(len(y) == 0):\n",
    "        return 1\n",
    "    t = np.mean(y == -1)\n",
    "    return 1 - t**2 - (1 - t)**2\n",
    "\n",
    "#定义impurty\n",
    "def lossfunc(theta, X, y):\n",
    "    '''\n",
    "    d为数据的维度，theta为decision stump的阈值\n",
    "    '''\n",
    "    y1 = y[X < theta]\n",
    "    y2 = y[X >= theta]\n",
    "    Gini1 = Gini(y1)\n",
    "    Gini2 = Gini(y2)\n",
    "    return len(y1) * Gini1 + len(y2) * Gini2\n",
    "\n",
    "def Generate_theta(X):\n",
    "    \"\"\"\n",
    "    生成阈值\n",
    "    \"\"\"\n",
    "    X = np.sort(X)\n",
    "    theta = (X[1:] + X[:-1]) / 2\n",
    "    theta = np.r_[[X[0] - 1], theta]\n",
    "    theta = np.r_[theta, [X[-1] + 1]]\n",
    "    \n",
    "    return theta\n",
    "\n",
    "#在d个维度上分别利用decision stump计算，找到损失函数的最小值，返回维度以及阈值\n",
    "def Decision_stump(X, y):\n",
    "    \"\"\"\n",
    "    对d个维度使用Decision_stump\n",
    "    \"\"\"\n",
    "    #获得数据维度\n",
    "    n, d = X.shape\n",
    "    #最终结果\n",
    "    Theta = 0\n",
    "    D = 0\n",
    "    Score = n\n",
    "    for i in range(d):\n",
    "        #取第d维的数据\n",
    "        x = X[:, i]\n",
    "        #计算阈值\n",
    "        theta = Generate_theta(x)\n",
    "        #遍历\n",
    "        for theta_ in theta:\n",
    "            #计算损失函数\n",
    "            score = lossfunc(theta_, x, y)\n",
    "            if score < Score:\n",
    "                Score = score\n",
    "                Theta = theta_\n",
    "                D = i\n",
    "    \n",
    "    return D, Theta, Score\n",
    "\n",
    "def isstop(X, y):\n",
    "    '''\n",
    "    判断是否停止，有两种情形，X全相同，另一种是所有数据都为一类\n",
    "    '''\n",
    "    n = X.shape[0]\n",
    "    #n1 = np.sum(y==-1)\n",
    "    n1 = np.sum(y!=y[0])\n",
    "    n2 = np.sum(X!=X[0, :])\n",
    "    return n1 == 0 or n2 == 0\n",
    "\n",
    "#构造树类\n",
    "class DTree:\n",
    "    def __init__(self, theta, d, value=None):\n",
    "        #阈值\n",
    "        self.theta = theta\n",
    "        #维度\n",
    "        self.d = d\n",
    "        #当前节点对应的值\n",
    "        self.value = value\n",
    "        #左右节点\n",
    "        self.left = None\n",
    "        self.right = None\n",
    "\n",
    "NUM = 0\n",
    "#构造学习函数\n",
    "def learntree(X, y):\n",
    "    global NUM\n",
    "    NUM += 1\n",
    "    if isstop(X, y):\n",
    "        #print(X.shape, y)\n",
    "        return DTree(None, None, y[0])\n",
    "    else:\n",
    "        d, theta, score = Decision_stump(X, y)\n",
    "        #print(d, theta, score)\n",
    "        tree = DTree(theta, d)\n",
    "        #划分数据\n",
    "        i1 = X[:, d] < theta\n",
    "        X1 = X[i1]\n",
    "        #print(\"X1\", X1.shape)\n",
    "        y1 = y[i1]\n",
    "        i2 = X[:, d] >= theta\n",
    "        X2 = X[i2]\n",
    "        y2 = y[i2]\n",
    "        #学习左树\n",
    "        leftTree = learntree(X1, y1)\n",
    "        #学习右树\n",
    "        rightTree = learntree(X2, y2)\n",
    "        #返回\n",
    "        tree.left = leftTree\n",
    "        tree.right = rightTree\n",
    "        \n",
    "        return tree\n",
    "    \n",
    "#预测函数\n",
    "def pred(tree, x):\n",
    "    #if tree.left == None and tree.right == None:\n",
    "    if tree.value != None:\n",
    "        return tree.value\n",
    "    if x[tree.d] < tree.theta:\n",
    "        return pred(tree.left, x)\n",
    "    else:\n",
    "        return pred(tree.right, x)\n",
    "    \n",
    "\n",
    "#计算误差\n",
    "def error(tree, X, y):\n",
    "    ypred = [pred(tree, x) for x in X]\n",
    "    return np.mean(ypred!=y)\n",
    "\n",
    "dtree = learntree(X_train, y_train)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0.0\n"
     ]
    }
   ],
   "source": [
    "#14\n",
    "print(error(dtree, X_train, y_train))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0.126\n"
     ]
    }
   ],
   "source": [
    "#15\n",
    "print(error(dtree, X_test, y_test))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAXQAAAD8CAYAAABn919SAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADl0RVh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PN7nvuPIRZLVwN8D76yqYY1SBs31S8AnkuwFLgY+lN6bw8adax/wz1X1WPcn583AayYg1zuAL1bVs1X1KPBvwLDevj3Ic3fcz/t5JTkCuAn4SFX9x5AyDZrr9cB7uuf95cA7k2yagFz76P31vqf7y/AfGPR5P6yLAwNcVJiid053Df9/UeHU/ba5iOdftLq2mz6V518U3cPwLqYNkuvIbvu3TdLjtd82H2O4F0UHebxWAbfTu/A4BXwFWDcBuS4BrqI3Cjsc2Amctly55mz7V7zwouj93eO2qps+agJyrQS2AheP43k/X6791l3AcC+KDvJ4rei2n+7mrwIuGijPsB/4JT4ovwF8g975tg93y/4YeGs3fSi9V2XsBv4TOGnOvh/u9rsXeMsk5AI+Qu/c651zfo4dd6797uNjDLHQh/B7/B16F2q/DnxiEnLRe9XB57tcO4EPLHOu19IbxT0NfBu4Z86+v9fl3Q28axJydb/DZ/d73r963Ln2u48LGGKhD+H3eDa9V3jdTa/wVw6SxXeKSlIjJuEcuiRpCCx0SWqEhS5JjbDQJakRFrokNcJCl6RGWOiS1AgLXZIa8X9dEtTKnXnbYwAAAABJRU5ErkJggg==\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "#16\n",
    "N = 300\n",
    "Ein = np.array([])\n",
    "tree = []\n",
    "m, n = train.shape\n",
    "for i in range(N):\n",
    "    index = np.random.randint(0, m, (m))\n",
    "    X1 = X_train[index, :]\n",
    "    y1 = y_train[index]\n",
    "    dtree = learntree(X1, y1)\n",
    "    tree.append(dtree)\n",
    "    Ein = np.append(Ein, error(dtree, X_train, y_train))\n",
    "    \n",
    "plt.hist(Ein)\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Ein = 0.05319999999999999\n"
     ]
    }
   ],
   "source": [
    "print(\"Ein = {}\".format(np.mean(Ein)))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAX0AAAD8CAYAAACb4nSYAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADl0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uIDMuMC4yLCBodHRwOi8vbWF0cGxvdGxpYi5vcmcvOIA7rQAAFrdJREFUeJzt3X9sXed93/H3l/eSlynp2LEsZ57lVHKtFlXQoHNYJcParLDXxM62qEXkQe6AaIABt1uNbciKzUEwN/H6j4u1BtZ57TTYg+duszxvRQVUqRvUbTd0rSI6/ql6Shg1iRQ7MRPZsiVHoih998c5V768upc8siiTPOf9Agiee85zeJ/Hh/7w0XOee57ITCRJzTCy0hWQJL1zDH1JahBDX5IaxNCXpAYx9CWpQQx9SWoQQ1+SGsTQl6QGMfQlqUHaK12BfldddVVu3LhxpashSWvKU0899d3MXL9UuVUX+hs3bmR6enqlqyFJa0pEfKNKOYd3JKlBDH1JahBDX5IaxNCXpAYx9CWpQSqFfkTcEhEHI2ImIu4ecPwjEfHliJiPiO19x3ZGxFfLr53LVXFJ0oVbMvQjogU8ANwKbAFuj4gtfcW+Cfwj4L/1nXsl8CvAh4CtwK9ExHsuvtqSpLejSk9/KzCTmYcycw54FNjWWyAzv56ZzwFn+879GPDFzDyama8CXwRuWYZ6n+flY9/n1//wIIdmj1+KHy9JtVAl9K8FDve8PlLuq6LSuRFxZ0RMR8T07OxsxR+90Owbp/jNJ2c4NHvibZ0vSU1QJfRjwL6qq6lXOjczd2XmVGZOrV+/5KeIBxptFU05fab/HxuSpK4qoX8EuK7n9QbgpYo//2LOvSBj7aIpc4a+JA1VJfT3A5sjYlNEjAE7gD0Vf/4TwEcj4j3lDdyPlvuW3VjZ05+bN/QlaZglQz8z54G7KML6ReCxzDwQEfdGxCcAIuInIuII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EZL/NTP/V7l7TV6XQW1Zq9elKzNfA/6EYkz/iojoLl3bW99zbSmPX0714ceh6hD6VRZuX7UiYiIiLutuAx8FXmDhYvM7gd9bmRq+LcPqvgf4VDlb5MPAse5ww2rVN7b9cxTXBoq27ChnWGwCNgNfeqfrN0g57vsg8GJm/kbPoTV3XYa1ZY1el/URcUW5/S7g71Dco/hjYHtZrP+6dK/XduDJLO/qXpSVvqO9HF8Usw++QjE+9tmVrs8F1v16itkGzwIHuvWnGLv7I+Cr5fcrV7quQ+r/3yn+eX2aomdyx7C6U/xz9YHyOj0PTK10/Su05ZGyrs+V/xNe01P+s2VbDgK3rnT9e+r1kxTDAM8Bz5RfH1+L12WRtqzF6/IB4Omyzi8A95T7r6f4wzQD/A+gU+4fL1/PlMevX456+IlcSWqQOgzvSJIqMvQlqUEMfUlqEENfkhrE0JekBjH0JalBDH1JahBDX5Ia5P8DSGu/v7KaU3gAAAAASUVORK5CYII=\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "#17\n",
    "def random_forest_error(tree, X, y):\n",
    "    \"\"\"\n",
    "    利用前k个树计算结果\n",
    "    \"\"\"\n",
    "    Error = np.array([])\n",
    "    N = len(tree)\n",
    "    for i in range(N):\n",
    "        E = []\n",
    "        for j in range(1+i):\n",
    "            E.append([pred(tree[j], x) for x in X])\n",
    "        E = np.array(E)\n",
    "        #0视为1\n",
    "        ypred = np.sign(E.sum(axis=0) + 0.5)\n",
    "        error = np.mean(ypred!=y)\n",
    "        Error = np.append(Error, error)\n",
    "    return Error\n",
    "\n",
    "Ein_G = random_forest_error(tree, X_train, y_train)\n",
    "\n",
    "plt.plot(np.arange(1, N+1), Ein_G)\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Ein = 0.002\n"
     ]
    }
   ],
   "source": [
    "print(\"Ein = {}\".format(np.mean(Ein_G)))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAX0AAAD8CAYAAACb4nSYAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADl0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uIDMuMC4yLCBodHRwOi8vbWF0cGxvdGxpYi5vcmcvOIA7rQAAIABJREFUeJzt3Xl8VPW9//HXZ7ashDVAICBhU1CRyqKi4NKq0Fbpoq1626rtld629v66eHvx0V5r7e3t7X6vrfdW+6tdtaJ2oy1VcWtdgaDsiIQ1YQ2EJQvZv/ePOYnJZGYyIcEhZ97PxyOPzJxz5uT7zUne853P+c4Zc84hIiKZIZDuBoiIyNtHoS8ikkEU+iIiGUShLyKSQRT6IiIZRKEvIpJBFPoiIhlEoS8ikkEU+iIiGSSU7gbEGjZsmBs3bly6myEi0q+sXr36kHOusLvtTrvQHzduHKWlpeluhohIv2Jmu1LZTuUdEZEMotAXEckgCn0RkQyi0BcRySAKfRGRDKLQFxHJIAp9EZEM4pvQr21o5vtPbeH13UfS3RQRkdOWb0K/vqmFe58tY13FsXQ3RUTktOWb0A8Fol1padUHvYuIJOKb0A8GDVDoi4gk45vQDwWiod+s0BcRScg3oR8MtI30W9PcEhGR05d/Qt800hcR6Y5vQj8QMMxU0xcRScY3oQ/Rur5G+iIiifkq9IMBo1WhLyKSkK9CPxQIaKQvIpJESqFvZvPNbIuZlZnZ4jjr55nZa2bWbGbXxaz7tpltNLPNZnavmXfG9RQIBkw1fRGRJLoNfTMLAvcBC4CpwI1mNjVms93ALcDDMY+dA1wMTAPOAWYBl/a61QlEa/qasikikkgqH4w+Gyhzzm0HMLNHgIXAprYNnHM7vXWxieuAbCACGBAGDvS61QlopC8iklwq5Z3RQHmH+xXesm45514BngP2eV9POu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b6CmRKvM2876ekL34I/QpgTIf7xcDeNLXlpDjn9nrfDwK/J/rHcKDt5bX3/WD6Wthjidre746Vc+6A94/aCvyEt0oFp31fzCxMNCgfcs79zlvc745NvH705+MC4Jw7CjxPtKY/yMxC3qqO7W3vi7d+IKmXHxPyQ+ivAiZ5Z8AjRE94LE1zm1JmZnlmNqDtNnAVsIFoH272NrsZ+GN6WnhSErV9KfAxb6bIhcCxtlLD6Sqmrv1+oscGon25wZthUQJMAla+3e1LxKv9/hTY7Jz7fodV/erYJOpHfzwuZlZoZoO82znAu4ieo3gOuM7bLPaYtB2r64BnnXdWt1fSfUa7L76Izjx4k2h97Mvpbk8P2z6e6GyDtcDGtvYTrd09A2z1vg9Jd1sTtP83RF9eNxEdmXwiUduJvly9zztO64GZ6W5/Cn35ldfWdd4/YVGH7b/s9WULsCDd7Y/pyyVESwHrgDXe17v727FJ0o9+d1yAacDrXps3AHd5y8cTfWIqAx4Dsrzl2d79Mm/9+L5oh96RKyKSQfxQ3hERkRQp9EVEMohCX0Qkgyj0RUQyiEJfRCSDKPRFRDKIQl9EJIMo9EVEMsj/AWbFhLRC1XAjAAAAAElFTkSuQmCC\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "#18\n",
    "Eout_G = random_forest_error(tree, X_test, y_test)\n",
    "\n",
    "plt.plot(np.arange(1, N+1), Eout_G)\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Eout = 0.07453666666666665\n"
     ]
    }
   ],
   "source": [
    "print(\"Eout = {}\".format(np.mean(Eout_G)))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAX0AAAD8CAYAAACb4nSYAAA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F4t9ds5lgLPNbAZwJ7AibDXv+5TfZzP7RWCvy43Uf4e/OGTV2Oyz563OuV1mNhe4z8yeL7PuhOxzXCr9KFNFxMlrZjYfwPu+11sei9fBzFrIBf7XnXPf9RbHep99zrlDwI/Inc+Y4U1rAoX7ld/n4LQn9d3ScXsrcJmZ7SQ3c+9F5Cr/OO8zzrld3ve95A7uq6nzezsuoR9lqog4CU578TFy7d7+8v/lnfV/E9Dnf2ycKixX0n8N2Oyc+5vAXXHe5zlehY+ZdQAXkzu5+SC5aU1g7D6Pmfakfls8fs65zzrnFjnnlpL7f33AOfdRYrzPZjbNzKb7PwOXAM9S7/f2ZJ/YqOEJkvcCL5BrC71usrenhvv1TXKzlY6QO/JfTa4t835gq/d9lreukevFtB3YCKya7O0/jv19G7mPsBuAp72v98Z8n88EnvL2+VngBm/5ycDPgG3At4E2b3m7d3ubd//Jk70P49z/dwDfj/s+e/v2jPf1nJ9T9X5va0SuiEgTiUvzjoiIRKDQFxFpIgp9EZEmotAXEWkiCn0RkSai0BcRaSIKfRGRJqLQFxFpIv8fRhefkuVYLEoAAAAASUVORK5CYII=\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "#19\n",
    "def learntree_new(X, y):\n",
    "    d, theta, score = Decision_stump(X, y)\n",
    "    tree = DTree(theta, d)\n",
    "    #划分数据\n",
    "    i1 = X[:, d] < theta\n",
    "    X1 = X[i1]\n",
    "    y1 = y[i1]\n",
    "    i2 = X[:, d] >= theta\n",
    "    X2 = X[i2]\n",
    "    y2 = y[i2]\n",
    "    #学习左树\n",
    "    leftTree = learntree(X1, y1)\n",
    "    #学习右树\n",
    "    rightTree = learntree(X2, y2)\n",
    "    \n",
    "    #左树\n",
    "    k1 = np.sign(np.sum(y1) + 0.5)#+0.5是为了防止出现0\n",
    "    leftTree = DTree(None, None, k1)\n",
    "    #右树\n",
    "    k2 = np.sign(np.sum(y2) + 0.5)\n",
    "    rightTree = DTree(None, None, k2)\n",
    "    #返回\n",
    "    tree.left = leftTree\n",
    "    tree.right = rightTree\n",
    "    return tree\n",
    "\n",
    "N = 500\n",
    "newtree = []\n",
    "m, n = train.shape\n",
    "for i in range(N):\n",
    "    index = np.random.randint(0, m, (m))\n",
    "    X1 = X_train[index, :]\n",
    "    y1 = y_train[index]\n",
    "    dtree = learntree_new(X1, y1)\n",
    "    newtree.append(dtree)\n",
    "    \n",
    "newEin_G = random_forest_error(newtree, X_train, y_train)\n",
    "\n",
    "plt.plot(np.arange(1, N+1), newEin_G)\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Ein = 0.11872000000000002\n"
     ]
    }
   ],
   "source": [
    "print(\"Ein = {}\".format(np.mean(newEin_G)))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAYQAAAD8CAYAAAB3u9PLAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADl0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uIDMuMC4yLCBodHRwOi8vbWF0cGxvdGxpYi5vcmcvOIA7rQAAIABJREFUeJzt3XmYXGWV+PHvqareO0sn3SSddFZIIGEL0gQQDQhBImiCDksQFRQn6gwzzuAGOoMOoz9cnhGHGVQYBQRRBNQhI4EIAVwgQDoQEpKQpLM3CUkn6Wy9Vled3x/3VvWt6qqu21v1UufzPP101XuXureX99x3F1XFGGOMCQz0BRhjjBkcLCAYY4wBLCAYY4xxWUAwxhgDWEAwxhjjsoBgjDEGsIBgjDHGZQHBGGMMYAHBGGOMKzTQF9Ad5eXlOnXq1IG+DGOMGVJWr159QFUrMu03pALC1KlTqampGejLMMaYIUVEdvrZ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Q1RnACvc9OPc/w/1aAvwkS9fY19qBL6nqLJyOCn/v/j6H8323Aher6pnAHGCBiJyHM1/YXe49N+DMJwZp5hUbgr4IbPS8H+73G/MBd263WBfT7P5tq+qw/QLOB5Z73t8G3DbQ19WH9zcVeMvzfhNQ6b6uBDa5r+8Frku131D+Ap4ELs2V+waKgdeBc3EGKYXc9PjfOU738PPd1yF3Pxnoa+/mfVbhZH4XA3/AmQlh2N6v5753AOVJaVn92x7WJQT8zcM0nIxT1b0A7vfYlOLD7ufgVg2cBbzKML9vt/pkDbAfeBbYChxWZ94wSLwvP/OKDXY/Ar4KRN33Yxne9xujwB9FZLWILHHTsvq37WscwhDmZx6mXDCsfg4iUgr8FvgnVT3axbyIw+K+VTUCzBGR0cDvgVmpdnO/D+l7FpEPA/tVdbWIXBRLTrHrsLjfJBeo6h5x1oZ5VkTe7mLffrnv4V5C8DMP03CyT0QqAdzvsTUmhs3PwR3d/lvgEVX9nZs87O8bQFUPAy/itJ+MdqeAgcT7it9zmnnFBrsLgIUisgNnqv2LcUoMw/V+41R1j/t9P07gn0uW/7aHe0CIz8Pk9kpYjDPv0nAVm1MK9/uTnvRPuT0TzgOOxIqhQ4k4RYGfAxtV9YeeTcP2vkWkwi0ZICJFwHycxtYXcOYNg873HPtZJMwrNhSo6m2qWqWqU3H+X59X1esZpvcbIyIlIjIi9hr4IPAW2f7bHuiGlCw01FwObMapd/3GQF9PH97Xr4G9QBjnaeEmnLrTFcAW9/sYd1/B6W21FVgHVA/09ffwnt+HUyxeC6xxvy4fzvcNnAG84d7zW8Dtbvp04DWgFngcKHDTC933te726QN9D72494uAP+TC/br396b7tT6WV2X7b9tGKhtjjAGGf5WRMcYYnywgGGOMASwgGGOMcVlAMMYYA1hAMMYY47KAYIwxBrCAYIwxxmUBwRhjDAD/H+1lNHiwmC61AAAAAElFTkSuQmCC\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "#20\n",
    "newEout_G = random_forest_error(newtree, X_test, y_test)\n",
    "\n",
    "plt.plot(np.arange(1, N+1), newEout_G)\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Eout = 0.15301599999999999\n"
     ]
    }
   ],
   "source": [
    "print(\"Eout = {}\".format(np.mean(newEout_G)))"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
